// pages/order/order.js
var common = require("../../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        business_id: "",
        class: 1,
        pageSuper: 1,
        nav: [ {
            id: 0,
            text: "全部"
        }, {
            id: 1,
            text: "待付款"
        }, {
            id: 2,
            text: "待核销"
        }, {
            id: 3,
            text: "已完成"
        }, {
            id: 4,
            text: "取消/关闭"
        } ],
        nav1: [ {
            id: "",
            text: "全部"
        }, {
            id: 0,
            text: "待付款"
        }, {
            id: 1,
            text: "待发货"
        }, {
            id: 2,
            text: "配送中"
        }, {
            id: 127,
            text: "已完成"
        }, {
            id: -1,
            text: "已退款"
        }, {
            id: -128,
            text: "已关闭"
        } ],
        choose_nav: 0,
        rows: 10,
        page: 1,
        order: [],
        supermarketOrders: [],
        goodsListStyle: {
            style: 4,
            styleConfig: {
                backgroundColor: "#fff"
            }
        }
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        //首次直接进入，拿user_id
        if (app.globalData.user) {
            //如果直接拿到openid
            this.setData({
                uid: app.globalData.user.uid
            });
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                this.setData({
                    uid: res.uid
                });
            });
        }
        var choose_nav = options.choose_nav || 0;
        this.setData({
            choose_nav: choose_nav,
            domain: app.globalData.domain.slice(0, -10)
        });
        this.setData({
            id: options.id
        });
        this.getBase();
        this.getBaseData();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        this.getBaseData();
    },
    share: function(e) {},
    chooseClass(e) {
        this.setData({
            class: e.currentTarget.dataset.id,
            page: 1,
            choose_nav: 0,
            pageSuper: 1,
            supermarketOrders: [],
            order: []
        });
        this.getBaseData();
    },
    getBaseData: function() {
        console.log(this.data.class);
        if (this.data.class == 1) {
            this.getSuperData();
        } else {
            this.getData();
        }
    },
    getSuperData: function() {
        var that = this;
        var choose_nav = this.data.choose_nav;
        // choose_nav=choose_nav==-1?"":choose_nav;
                common.request({
            endpoint: "v1/Supermarket/order",
            method: "post",
            data: {
                page: that.data.pageSuper,
                status: choose_nav,
                order_sn: that.data.search_con,
                business_id: that.data.id
            },
            success: res => {
                that.setData({
                    supermarketOrders: that.data.supermarketOrders.concat(res.extra.rows),
                    pageSuper: that.data.pageSuper + 1
                });
                let spOrder = res.extra.rows;
                this.data.supermarketOrders.forEach((item, index) => {
                    item.items.forEach((i, q) => {
                        if (i.status == 1 || i.status == 127 || i.status == -1 || i.status == -128) {
                            spOrder[index].status_detail = 1;
                        } else {
                            spOrder[index].status_detail = 0;
                        }
                        if (i.status == 0) {
                            spOrder[index].status_confirm = 1;
                        } else {
                            spOrder[index].status_confirm = 0;
                        }
                        if (i.status == 2) {
                            spOrder[index].status_delivery = 1;
                        } else {
                            spOrder[index].status_delivery = 0;
                        }
                    });
                });
                this.setData({
                    supermarketOrders: spOrder
                });
                console.log(spOrder);
            }
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                var goodsListStyle = that.data.goodsListStyle;
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    timeStyle: {
                        height: 32,
                        mainColor: res.extra.color_main
                    },
                    goodsListStyle: {
                        ...goodsListStyle,
                        mainColor: res.extra.color_main
                    }
                });
            }
        });
    },
    choose_nav: function(e) {
        var id = e.currentTarget.dataset.id;
        console.log(id);
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        this.setData({
            choose_nav: id,
            page: 1,
            order: [],
            pageSuper: 1,
            supermarketOrders: []
        });
        that.getBaseData();
    },
    getData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        var choose_nav = that.data.choose_nav;
        var page = that.data.page;
        common.request({
            endpoint: "Localbusiness/order/business-order",
            method: "post",
            data: {
                user_id: that.data.uid ? that.data.uid : "402",
                status: choose_nav,
                rows: that.data.rows,
                page: page,
                id: that.data.id
            },
            success: res => {
                var order = res.extra.rows;
                order.map(x => {
                    var actually_price = Number(x.sale_price) - Number(x.platform_commission_price) - Number(x.leader_commission_price);
                    x.actually_price = actually_price.toFixed(2);
                });
                that.setData({
                    order: that.data.order.concat(res.extra.rows),
                    page: that.data.page + 1
                });
            }
        });
    },
    recieve: function(e) {
        let that = this;
        // console.log(e);
        // return;
                common.request({
            endpoint: "v1/Supermarket/order/receive-confirm?order_sn=" + e.target.dataset.item.order_sn,
            method: "post",
            data: {},
            success: function(res) {
                if (res.code == 0) {
                    wx.showModal({
                        title: "提示",
                        content: "收货成功",
                        showCancel: false,
                        success(res) {
                            wx.navigateTo({
                                url: "/localLifePackage/pages/myOrder/myOrder"
                            });
                        }
                    });
                } else {
                    wx.showModal({
                        title: "提示",
                        content: "提交失败",
                        showCancel: false,
                        success(res) {
                            wx.navigateTo({
                                url: "/localLifePackage/pages/myOrder/myOrder"
                            });
                        }
                    });
                }
            }
        });
    },
    //立即付款
    pay: function(e) {
        let that = this;
        that.setData({
            payBtn: true
        });
        var order_sn = e.currentTarget.dataset.ddh;
        common.request({
            endpoint: "v1/Supermarket/order/do-pay",
            method: "post",
            data: {
                order_sn: order_sn
            },
            success: res => {
                wx.requestPayment({
                    timeStamp: res.extra.pay.timeStamp,
                    nonceStr: res.extra.pay.nonceStr,
                    package: res.extra.pay.package,
                    signType: res.extra.pay.signType,
                    paySign: res.extra.pay.paySign,
                    success: function(res) {
                        wx.showToast({
                            title: "支付成功",
                            icon: "success",
                            mask: true,
                            duration: 3e3,
                            success: function(res) {
                                console.log("支付成功");
                                wx.showToast({
                                    title: "购买成功！",
                                    icon: "success",
                                    success: function() {
                                        common.request({
                                            endpoint: "v1/Supermarket/order/sync-notify",
                                            method: "post",
                                            data: {
                                                order_sn: order_sn
                                            },
                                            success: res => {
                                                setTimeout(function() {
                                                    wx.navigateTo({
                                                        url: "/localLifePackage/pages/myOrder/myOrder"
                                                    });
                                                }, 500);
                                            },
                                            fail(err) {
                                                wx.showModal({
                                                    title: "提示",
                                                    content: err.message
                                                });
                                            }
                                        });
                                        wx.showToast({
                                            title: "支付成功",
                                            icon: "success"
                                        });
                                    }
                                });
                            }
                        });
                    },
                    fail: function(res) {
                        console.log(res);
                        wx.navigateTo({
                            url: "/localLifePackage/pages/myOrder/myOrder"
                        });
                    },
                    complete: function(res) {
                        console.log("complete");
                        //解禁立即支付按钮
                                                that.setData({
                            payBtn: false
                        });
                    }
                });
            },
            fail: function(res) {
                //解禁立即支付按钮
                that.setData({
                    payBtn: false
                });
                wx.showModal({
                    title: "提示",
                    content: res.message,
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            that.setData({
                                page: 1
                            });
                            that.getBaseData();
                        }
                    }
                });
            }
        });
    },
    onPageTap: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    toorder_detail: function(e) {
        let orderSn = e.currentTarget.dataset.ordersn;
        wx.navigateTo({
            url: "/localLifePackage/pages/order_detail/order_detail?order_sn=" + orderSn
        });
    },
    againOrder: function(e) {
        wx.navigateTo({
            url: "/localLifePackage/pages/newPlace_order/newPlace_order?id=" + e.currentTarget.dataset.id
        });
    },
    againOrderCode: function(e) {
        wx.navigateTo({
            url: "/localLifePackage/pages/writeCodeDetail/writeCodeDetail?order_sn=" + e.currentTarget.dataset.ordersn
        });
    }
});